/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: EWALTERS
 * Created: 10:01:20 AM, Aug 1, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet.analysis.ast;

import java.util.Collection;
import java.util.Hashtable;

import com.mitre.spmif2opnet.analysis.AnalysisException;
import com.mitre.spmif2opnet.analysis.Identifier;


/**
 * This class represents an abstract OPNET scenario, or a set of tasks on the
 * same network.
 */
public class Scenario {
	/**
	 * Scenario name
	 */
	private String _name;
	/**
	 * Interarrival time for the threads in this scenario
	 */
	private double _interarrival_time;
	/**
	 * Number of jobs in this scenario
	 */
	private int    _num_jobs;
	/**
	 * Scenario priority
	 */
	private int    _priority;
	/**
	 * File that contains this priority
	 */
	private String _model_file;
	/**
	 * Sub-graphs in this scenario
	 */
	private Hashtable<String, Graph> _tasks;
	/** 
	 * Graphs to start with
	 */
	private String _start_graph;
	/** 
	 * Units of service
	 */
	private ServiceUnitType _service_units;

	/**
	 * Default constructor
	 * @param name 
	 * @param start_graph
	 * @param int_time
	 * @param num_jobs
	 * @param priority
	 * @param model_file
	 * @param service_units
	 */
	public Scenario(String name, String start_graph, double int_time, int num_jobs, int priority, String model_file, String service_units) {
		_name = name;
		_start_graph = start_graph;
		_service_units = ServiceUnitType.fromString(service_units);
		_interarrival_time = _service_units.normalize(int_time);
		_num_jobs = num_jobs;
		_priority = priority;
		_model_file = model_file;
		_tasks = new Hashtable<String, Graph>();
	}
	
	/**
	 * Name accessor
	 * @return the name
	 */
	public String getName() {
		return _name;
	}
		
	/**
	 * Add a task/graph to the scenario
	 * @param t the graph
	 */
	public void addTask(Graph t) {
		_tasks.put(t.getId(), t);
	}
	
	/**
	 * Get the start graph of this scenario
	 * @return the start graph
	 * @throws AnalysisException
	 */
	public Graph getStartGraph() throws AnalysisException {
		return getTask(_start_graph);
	}
	
	/**
	 * Get a graph in this scenario from a string
	 * @param id the string
	 * @return the graph
	 * @throws AnalysisException
	 */
	public Graph getTask(String id) throws AnalysisException {
		Graph t = _tasks.get(id);
		if (t == null) {
			throw new AnalysisException("Attempt to access non-existent task " 
					+ id + " from scenario " + _name);
		}
		return t;
	}
	
	/**
	 * Get a graph in this scenario from an identifier
	 * @param id the id
	 * @return the graph
	 * @throws AnalysisException
	 */
	public Graph getTask(Identifier id) throws AnalysisException {
		if (!id.getScenarioName().equals(_name)) {
			throw new AnalysisException("Attempt to access scenario " 
					+ _name + " with incorrect identifier " 
					+ id.toString());
		}
		return this.getTask(id.getGraphId());
	}
	
	/**
	 * Get a node in this scenario from an identifier
	 * @param id the id
	 * @return the node
	 * @throws AnalysisException
	 */
	public Node getNode(Identifier id) throws AnalysisException {
		Graph t = this.getTask(id);
		return t.getNode(id);
	}
	
	/**
	 * Get all of the sub-graphs in this scenario
	 * @return the graphs
	 */
	public Collection<Graph> getAllTasks() {
		return _tasks.values();
	}

	/**
	 * Priority accessor
	 * @return the priority
	 */
	public int getPriority() {
		return _priority;
	}

	/**
	 * Interarrival time accessor
	 * @return the time
	 */
	public double getInterarrivalTime() {
		return this._interarrival_time;
	}
	
	/**
	 * File name accessor
	 * @return the name
	 */
	public String getFileName() {
		return _model_file;
	}
	
	/**
	 * number of jobs accessor
	 * @return the number
	 */
	public int getNumJobs() {
		return _num_jobs;
	}
}

